#!/usr/bin/env bash
set -eu

version=0.9.12
arch="$(uname -m)"
tarball="uv-$arch-unknown-linux-gnu.tar.gz"
declare -A sha256=(
    [aarch64]=3c4dd18c1db6bd1af3b84ea3b3cc34dd9d8b955d3e700d1e1e4a18249decbe69
    [x86_64]=4e43dac0c82b12b66564d91d5649350377b771d1df84374d9be1b9e6c8dd8152
)

check_version() {
    out="$(uv --version)" && [ "$out" = "uv $version" ]
}

if ! check_version 2>/dev/null; then
    set -x
    tmpdir="$(mktemp -d)"
    trap 'rm -r "$tmpdir"' EXIT
    cd "$tmpdir"
    curl -fLO --retry 3 "https://github.com/astral-sh/uv/releases/download/$version/$tarball"
    sha256sum -c <<<"${sha256[$arch]} $tarball"
    tar -xzf "$tarball" --no-same-owner --strip-components=1 -C /usr/local/bin "uv-$arch-unknown-linux-gnu"/{uv,uvx}
    check_version
fi
